#pragma once

#include<iostream>
#include<vector>
#include<string>
#include<ctemplate/template.h>
#include"oj_model.hpp"
// #include"oj_model2.hpp"

namespace ns_view
{
    const std::string template_path = "./ctemplate/";
    using namespace ns_model;
    class View
    {
    public:
        View() {}
        ~View() {}
    public:
        void AllExpandHtml(const std::vector<struct Question> &questions,std::string *html)
        {
            // std::cout << "11111" << std::endl;
            //1.形成网页html路径
            std::string html_path = template_path + "AllQuestions.html";
            //2.ctemplate形成数据字典
            ctemplate::TemplateDictionary root("AllQuestions");
            for(const auto&q : questions)
            {
                //形成子数据字典
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
                // std::cout << q.number  << " "<< q.title << " " <<  q.star << std::endl;
                sub->SetValue("number",q.number);
                sub->SetValue("title",q.title);
                sub->SetValue("star",q.star);
            }
            //3.ctemplate渲染网页html，获取渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(html_path,ctemplate::DO_NOT_STRIP);
            //4.开始渲染
            tpl->Expand(html, &root);
        }

        void OneExpandHtml(const struct Question &question,std::string *html)
        {
            //1.形成网页html路径
            std::string html_path = template_path + "OneQuestion.html";
            //2.ctemplate形成数据字典
            ctemplate::TemplateDictionary root("OneQuestion");
            root.SetValue("number",question.number);
            root.SetValue("title",question.title);
            root.SetValue("star",question.star);
            root.SetValue("desc",question.desc);
            root.SetValue("header",question.header);
            //3.ctemplate渲染网页html，获取渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(html_path,ctemplate::DO_NOT_STRIP);
            //4.开始渲染
            tpl->Expand(html, &root);
        }

        void AllErrorHtml(std::string *html)
        {
            //1.形成网页html路径//1.形成网页html路径
            std::string html_path = template_path + "AllErr.html";
            
            //2.ctemplate形成数据字典
            ctemplate::TemplateDictionary root("AllErr");

            //3.ctemplate渲染网页html，获取渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(html_path,ctemplate::DO_NOT_STRIP);

            //4.开始渲染
            tpl->Expand(html, &root);

        }

        void OneErrorHtml(const std::string &number,std::string *html)
        {
            //1.形成网页html路径
            std::string html_path = template_path + "OneErr.html";
           
            //2.ctemplate形成数据字典
            ctemplate::TemplateDictionary root("OneErr");
            root.SetValue("number",number);

            //3.ctemplate渲染网页html，获取渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(html_path,ctemplate::DO_NOT_STRIP);

            //4.开始渲染
            tpl->Expand(html, &root);
        }
    };
}